package com.eng.carro.mb;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.eng.carro.entities.Order;
import com.eng.carro.services.OrderService;

@ManagedBean(name = "almoxMB")
@SessionScoped
public class AlmoxMBean implements Serializable {

  private static final long serialVersionUID = 949596940654078882L;

  public Order selectedOrder;

  @EJB
  OrderService service;

  private List<Order> values;

  public List<Order> getValues() {
//	if (values == null) {
	  values = service.ordersByPriority();
//	}

	return values;
  }

  public void atualizar() {
	try {
	  selectedOrder.setStatus(3);
	  service.makeUpdate(selectedOrder);
	  values = null;
	} catch (Exception e) {
	  e.printStackTrace();
	}
  }
  
  public String getStatusNome() {
	if ( selectedOrder == null  ) return "";
	return Order.STATUS[selectedOrder.getStatus()];
  }
  
  public String getPrioridadeTexto() {
	if ( selectedOrder == null  ) return "";
	return Order.PRIORITY[selectedOrder.getPriority()];
  }
  
  public String getPrioridadeTexto(Integer i) {
	return Order.PRIORITY[i];
  }
  
  public void setValues(List<Order> values) {
	this.values = values;
  }

  public void enviar(Order order) {
	try {
	  if (order != null) {
		selectedOrder = order;
	  }
	} catch (Exception e) {
	  e.printStackTrace();
	}
  }

  public Order getSelectedOrder() { return selectedOrder; }
  public void setSelectedOrder(Order selectedOrder) { this.selectedOrder = selectedOrder; }
}
